package actions;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import donnees.Panier;

import metier.*;

public class ActionAjouterAuPanier extends UnicastRemoteObject implements IAction {
	private static final long serialVersionUID = 8471152363767676285L;

	public ActionAjouterAuPanier() throws RemoteException {
		super();
	}
	
	@Override
	public String execute(HttpServletRequest req) throws Exception {

		HttpSession theSession=req.getSession(true);

		//si le panier est null, on le cree
		if(theSession.getAttribute("Panier")==null){
			theSession.setAttribute("Panier", new Panier());
		}
		if(req.getParameter("idProd")!=null){
			//on appelle le metier et on ajoute le produit au panier
			IMetier metier=(IMetier)Naming.lookup("rmi://localhost:2000/ServeurMetier");
			theSession.setAttribute("Panier",metier.ajouterAuPanier((Panier)theSession.getAttribute("Panier"), Integer.parseInt(req.getParameter("idProd"))));
		}
		return (String)theSession.getAttribute("currentPage"); //on reste sur la meme page
	}
}
